package com.saicmotor.pay;

import com.saicmotor.pay.http.BaseResponse;
import com.saicmotor.pay.http.Entity.QueryTranStatusEntity;
import com.saicmotor.pay.http.Entity.appendPayRecordEntity;
import com.saicmotor.pay.http.PayApiManager;
import com.saicmotor.pay.http.ResultCallback;
import com.saicmotor.pay.http.request.appendPayRecordReq;

import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

public class PayManager {
    private static class SingletonHolder {
        private static final PayManager INSTANCE = new PayManager();
    }

    PayApiManager mHttpAPIManager;

    private PayManager() {
        mHttpAPIManager = PayApiManager.getInstance();

    }

    private int i = 0;

    public static PayManager getInstance() {
        return SingletonHolder.INSTANCE;
    }

    public void appendPayRecord(String transId,
                                String platformMerchId,
                                String merchOrderId,
                                String userId,
                                String userName,
                                Float orderAmt,
                                Float transAmt,
                                String goodsDescription,
                                String timeExpire,
                                String payScene,
                                List<appendPayRecordReq.payDetails> payDetails,
                                String payerClientIp, ResultCallback<appendPayRecordEntity> resultCallBack) {
        mHttpAPIManager.appendPayRecord(transId,
                platformMerchId,
                merchOrderId,
                userId,
                userName,
                orderAmt,
                transAmt,
                goodsDescription,
                timeExpire,
                payScene,
                payDetails,
                payerClientIp)
                .subscribeOn(Schedulers.io())               // 切换到IO线程进行网络请求
                .observeOn(AndroidSchedulers.mainThread())  // 切换回到主线程 处理请求结果
                .subscribe(new Observer<BaseResponse<appendPayRecordEntity>>() {
                    @Override
                    public void onSubscribe(@NotNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NotNull BaseResponse<appendPayRecordEntity> appendPayRecordEntityBaseResponse) {
                        if (resultCallBack != null) {
                            if (appendPayRecordEntityBaseResponse.isSuccessful()) {
                                resultCallBack.onSuccess(appendPayRecordEntityBaseResponse.getData());
                            } else {
                                resultCallBack.onError(appendPayRecordEntityBaseResponse.getResultCode(), appendPayRecordEntityBaseResponse.getMessage());
                            }
                        }
                    }

                    @Override
                    public void onError(@NotNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void queryTranStatus(String merchOrderId, String platformMerchId, String transId, ResultCallback<QueryTranStatusEntity> resultCallBack) {
        mHttpAPIManager.queryTranStatus(merchOrderId, platformMerchId, transId)
                .subscribeOn(Schedulers.io())               // 切换到IO线程进行网络请求
                .observeOn(AndroidSchedulers.mainThread())  // 切换回到主线程 处理请求结果
                .subscribe(new Observer<BaseResponse<QueryTranStatusEntity>>() {
                    @Override
                    public void onSubscribe(@NotNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NotNull BaseResponse<QueryTranStatusEntity> queryTranStatusEntityBaseResponse) {
                        if (resultCallBack != null) {
                            if (queryTranStatusEntityBaseResponse.isSuccessful()) {
                                resultCallBack.onSuccess(queryTranStatusEntityBaseResponse.getData());
                            } else {
                                resultCallBack.onError(queryTranStatusEntityBaseResponse.getResultCode(), queryTranStatusEntityBaseResponse.getMessage());
                            }
                        }
                    }

                    @Override
                    public void onError(@NotNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

}
